package edu.boun.g4.coursity.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.boun.g4.coursity.controller.Response;
import edu.boun.g4.coursity.domain.Comment;
import edu.boun.g4.coursity.domain.Course;
import edu.boun.g4.coursity.domain.CoursityUser;
import edu.boun.g4.coursity.domain.Rating;
import edu.boun.g4.coursity.dto.RatingDto;

/**
 * @author sebnemarslan
 * @author mertusta
 * @author alifindik
 * 
 */
@Service("ratingService")
@Transactional
public class RatingService {

	protected static Logger logger = Logger.getLogger("service");

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/**
	 * Retrieves all ratings
	 * 
	 * @return a list of ratings
	 */
	@SuppressWarnings("unchecked")
	public List<Rating> getAll() {
		logger.debug("Retrieving all ratings");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  Rating");

		// Retrieve all
		return query.list();
	}	

	
	/**
	 * Retrieves a single rating
	 */
	public Rating get(Integer id) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing rating first
		Rating rating = (Rating) session.get(Rating.class, id);

		return rating;
	}

	/**
	 * Adds a new rating
	 */
	public void add(Rating rating) {
		logger.debug("Adding new rating");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Persists to db
		session.save(rating);
	}

	/**
	 * Adds a new rating
	 */
	public void add(Integer courseId, Integer userId, Rating newRating) {
		logger.debug("Adding new rating");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
	
		// Add to course as well
		// Retrieve existing course via id
		Course existingCourse = (Course) session.get(Course.class, courseId);
		newRating.setCourse(existingCourse);

		
		// Add to user as well
		// Retrieve existing user via id
		CoursityUser existingUser = (CoursityUser) session.get(CoursityUser.class, userId);
		newRating.setCousityUser(existingUser);
		
		// Persists to db
        session.save(newRating);
	}

	
	/**
	 * Deletes an existing rating
	 * 
	 * @param id
	 *            the id of the existing rating
	 */
	public void delete(Integer id) {
		logger.debug("Deleting existing rating");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing rating first
		Rating rating = (Rating) session.get(Rating.class, id);

		// Delete
		session.delete(rating);
	}

	/**
	 * Edits an existing rating
	 */
	public void edit(Rating rating) {
		logger.debug("Editing existing rating");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing rating via id
		Rating existingRating = (Rating) session.get(Rating.class,
				rating.getId());

		// Assign updated values to this rating
		existingRating.setRatingScore((rating.getRatingScore()));

		// Save updates
		session.save(existingRating);
	}		
	
	
}
